package com.jiyun.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiyun.mapper.StoreMapper;
import com.jiyun.mapper.UserMapper;
import com.jiyun.pojo.Store;
import com.jiyun.pojo.User;
import com.jiyun.service.StoreService;
import com.jiyun.service.UserService;
import common.models.dtos.StoreDto;
import common.models.dtos.TimeDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.*;

@Service
public class StoreServiceImpl extends ServiceImpl<StoreMapper, Store> implements StoreService {


    /**
     * 门店查询
     * @param dto
     * @return
     */
    @Override
    public List<Store> findAllAndLike(StoreDto dto) {
        if (dto==null){
            throw new RuntimeException("参数有误");
        }
        LambdaQueryWrapper<Store> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(dto.getStoreId()!=null,Store::getUserId,dto.getStoreId()); //只查询当前专员下的门店信息
        queryWrapper.like(!StringUtils.isEmpty(dto.getName()),Store::getStoreName,dto.getName());       //模糊姓名
        queryWrapper.gt(!StringUtils.isEmpty(dto.getStartTime()),Store::getJoiningTime,dto.getStartTime()); //开始时间
        queryWrapper.lt(!StringUtils.isEmpty(dto.getEndTime()),Store::getJoiningTime,dto.getEndTime()); //结束时间
        queryWrapper.ge(!StringUtils.isEmpty(dto.getRating()),Store::getRating,dto.getRating());//星级查询
        queryWrapper.eq(!StringUtils.isEmpty(dto.getRegionLike()),Store::getRegion,dto.getRegionLike()); //区域查询
        List<Store> storeList = this.list(queryWrapper);
        return storeList;
    }

    /**
     * 异常门店查询
     * @param dto
     * @return
     */
    @Override
    public Map<String, Object> findTime(TimeDto dto) {
        //获取某一天的异常门店数量
        Date time = dto.getTime();
        Date stateTime = dto.getStateTime();
        Date endTime = dto.getEndTime();
//        System.out.println(time);
//        System.out.println(stateTime);
//        System.out.println(endTime);

        Map<String,Object> map=new HashMap<>();
        //获取门店数量
        Integer count = 0;
        List<Store> list = this.list();
        for (Store store : list) {
            if (store.getExTime()!=null){
                count++;
            }
        }
        map.put("总异常门店数量",count);
        //获取今日异常门店数量
        LocalDate nowDate = LocalDate.now();
        LambdaQueryWrapper<Store> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Store::getExTime,nowDate);
        Integer newDateEx = this.count(queryWrapper);
        map.put("当天异常门店数量",newDateEx);


        LambdaQueryWrapper<Store> queryWrapper1=new LambdaQueryWrapper<>();
        queryWrapper1.eq(Store::getExTime,time);
        Integer date2 = this.count(queryWrapper1);
        map.put("查询某一天的异常数量",date2);
        //范围异常查询
        LambdaQueryWrapper<Store> queryWrapper2=new LambdaQueryWrapper<>();
//        queryWrapper1.ge(!StringUtils.isEmpty(dto.getStateTime()),Store::getExTime,dto.getStateTime());
        queryWrapper1.ge(Store::getExTime,dto.getStateTime());
//        queryWrapper1.le(!StringUtils.isEmpty(dto.getEndTime()),Store::getExTime,dto.getEndTime());
        queryWrapper1.le(Store::getExTime,dto.getEndTime());
        Integer count1 = this.count(queryWrapper2);
        if (stateTime==null && endTime==null){
            count1=0;
        }
        map.put("范围异常查询异常门店数量",count1);

        //按季度查询门店异常数量
        Integer size = dto.getSize();
        //那年的季度
        Integer year = dto.getYear();
        LambdaQueryWrapper<Store> queryWrapper3=new LambdaQueryWrapper<>();
        if (size==1){
           //查询第一季度的门店异常数量
            Calendar calendar=Calendar.getInstance();
            calendar.set(year,00,01);
            Date time1 = calendar.getTime();
            queryWrapper3.gt(Store::getExTime,time1);
            calendar.set(year,02,31);
            Date time2 = calendar.getTime();
        queryWrapper3.lt(Store::getExTime,time2);
            Integer quarterInter = this.count(queryWrapper3);
            map.put("第"+year+"第一季度查询过期门店",quarterInter);
        }else if (size==2){
//查询第二季度的门店异常数量
            Calendar calendar=Calendar.getInstance();
            calendar.set(year,03,01);
            Date time1 = calendar.getTime();
            calendar.set(year,05,30);
            Date time2 = calendar.getTime();
            queryWrapper3.ge(Store::getExTime,time1).le(Store::getExTime,time2);
            Integer quarterInter = this.count(queryWrapper3);
            map.put("第"+year+"年第二季度查询过期门店",quarterInter);
        }else if (size==3){
//查询第三季度的门店异常数量
            Calendar calendar=Calendar.getInstance();
            calendar.set(year,06,01);
            Date time1 = calendar.getTime();
            calendar.set(year,8,31);
            Date time2 = calendar.getTime();
            queryWrapper3.ge(Store::getExTime,time1).le(Store::getExTime,time2);
            Integer quarterInter = this.count(queryWrapper3);
            map.put("第"+year+"年第三季度查询过期门店",quarterInter);
        }else if (size==4){
//查询第四季度的门店异常数量
            Calendar calendar=Calendar.getInstance();
            calendar.set(year,9,01);
            Date time1 = calendar.getTime();
            calendar.set(year,11,31);
            Date time2 = calendar.getTime();
            queryWrapper3.ge(Store::getExTime,time1).le(Store::getExTime,time2);
            Integer quarterInter = this.count(queryWrapper3);
            map.put("第"+year+"年第四季度查询过期门店",quarterInter);
        }
        //根据月份进行查询
        if (year!=null && dto.getMonth()!=null){
            LambdaQueryWrapper<Store> queryWrapper4=new LambdaQueryWrapper<>();
            //根据月份进行查询
            LocalDate date3 = LocalDate.of(year, dto.getMonth(), 8);
            LocalDate firstDay = date3.with(TemporalAdjusters.firstDayOfMonth()); // 获取当前月的第一天
            LocalDate lastDay = date3.with(TemporalAdjusters.lastDayOfMonth()); // 获取当前月的最后一天
            queryWrapper4.ge(Store::getExTime,firstDay);
            queryWrapper4.le(Store::getExTime,lastDay);
            int count2 = this.count(queryWrapper);
            map.put("第"+year+"年-"+dto.getMonth()+"月的异常门店数量",count2);
        }
        return map;
    }
}